/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ambimmort.ucagent.appframework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 定巍
 */
public class ModuleManager {

    private static ModuleManager instance = null;

    private Map<Class, Module> moduleRegistries = new HashMap<Class, Module>();
    private List<Module> moduleList = new ArrayList<Module>();

    private Map<Class, Thread> threads = new HashMap<Class, Thread>();

    private ModuleManager() {

    }

    public static ModuleManager getInstance() {
        if (instance == null) {
            instance = new ModuleManager();
        }
        return instance;
    }

    public void registerModule(Module comp) {
        moduleList.add(comp);
        moduleRegistries.put(comp.getClass(), comp);
    }

    public static <D extends Module> D lookup(Class<D> clazz) {
        return (D) ModuleManager.getInstance().moduleRegistries.get(clazz);
    }

    public void startAll() {
        for (final Module comp : moduleList) {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    comp.start();
                }
            }).start();

            System.out.println(comp.getName() + " initialized.");
        }
        System.out.println("All components initialized.");
    }

}
